export default {
	data() {
			return {
				/*当前页显示数量*/
				pageSize: 10,
				/*每页显示的数量*/
				pageSizes: [10, 20, 50, 100],
				/*dataTotal表数量*/
				dataTotal: 0,
				/*分页器*/
				currentPage: 1,
				// 通用SRC
				SRC: '',
				// 通信方式ＬｉｓＴ
				notifyTypeList: [{
					value: '微信',
					label: '微信'
				}, {
					value: '手机号',
					label: '手机号'
				}, {
					value: '语音号',
					label: '语音号'
				}, {
					value: '邮箱',
					label: '邮箱'
				}],
				// 存放游戏列表
				games: [],
				// 添加的表
				ruleForm: {
					/*game: '',
					gameId: 0,
					gameName: '',*/
					addName: '',
					addWay: '',
					addVoice: '',
					addDesc: '',
					notifyType: ''
				},
				// 修改数据
				revampForm: {
					/*game: '',
					gameId: 0,
					gameName: '',*/
					addName: '',
					addWay: '',
					addVoice: '',
					addDesc: '',
					notifyType: ''
				},
				// 添加表的宽度
				formLabelWidth: "120px",
				rules: {
					addName: [{
						min: 1,
						max: 50,
						message: '长度在 3 到 50 个字符',
						trigger: 'blur'
					}],
					addVoice: [{
						min: 3,
						max: 50,
						message: '长度在 3 到 50 个字符',
						trigger: 'blur'
					}],
					addDesc: [{
						min: 3,
						max: 50,
						message: '长度在 3 到 50 个字符',
						trigger: 'blur'
					}]
				},
				revampShow: false,
				tableHeader: [{
					"prop": "id",
					"label": "序列号"
				}, {
					"prop": "groupName",
					"label": "分组名称"
				}, {
					"prop": "notifyType",
					"label": "通知方式"
				}, {
					"prop": "voiceTemplate",
					"label": "语音模板"
				}, {
					"prop": "description",
					"label": "分组描述"
				}],
				// 表格展示的数据
				tableData: [],
				// 表格所有数据
				tableDataAll: []
			}
		},
		methods: {
			// 分页器
			// 控制每页的数量
			handleSizeChange(val) {
				this.pageSize = val;
				this.tableControl();
			},
			// 页数
			handleCurrentChange(val) {
				this.currentPage = val;
				this.tableControl();
			},
			tableControl() {
				this.tableData = this.tableDataAll.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
			},
			// 通用 成功 与 失败
			successFun(data) {
				this.$message({
					message: data,
					type: 'success'
				});
			},
			errorFun(data) {
				this.$message.error(data);
			},
			// 切换通知方式
			notifyChange(val) {
				this.ruleForm.notifyType = '';
				for (var i = 0; i < val.length; i++) {
					this.ruleForm.notifyType += val[i] + "-";
				}
				this.ruleForm.notifyType = this.ruleForm.notifyType.substring(0, this.ruleForm.notifyType.length - 1);
			},
			// 修改切换通知方式
			notifyRevamp(val) {
				this.revampForm.notifyType = '';
				for (var i = 0; i < val.length; i++) {
					this.revampForm.notifyType += val[i] + "-";
				}
				this.revampForm.notifyType = this.revampForm.notifyType.substring(0, this.revampForm.notifyType.length - 1);
			},
			// 添加组
			addForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.addData();
						this.getTableData("");
					} else {
						return false;
					}
				});
			},
			// 查寻
			findForm(ruleForm) {

			},
			//重置
			reset(form) {
				this.$refs[form].resetFields();
				this.ruleForm.gameName = '',
				this.ruleForm.gameId = "",
				this.ruleForm.game = "",
				this.getTableData("");
			},
			resetForm(form) {
				this.$refs[form].resetFields();
			},
			// 游戏切换
			gameChange(val) {
				this.ruleForm.gameId = val;
				for (var i in this.games) {
					if (val === this.games[i]['id']) {
						this.ruleForm.gameName = this.games[i]['projectName'];
					}
				}
			},
			// 修改游戏切换
			gameRevamp(value) {
				this.revampForm.gameId = value;
				for (var i in this.games) {
					if (value === this.games[i]['id']) {
						this.revampForm.gameName = this.games[i]['projectName'];
					}
				}
			},
			// 添加数据
			addData() {
				var obj = {
					/*gameName: this.ruleForm.gameName,*/
					gameId: 0,
					groupName: this.ruleForm.addName,
					voiceTemplate: this.ruleForm.addVoice,
					description: this.ruleForm.addDesc,
					notifyType: this.ruleForm.notifyType
				}
				this.$http.post(`${this.SRC}monitorgroup/add.do`, obj, {
					emulateJSON: true
				}).then(function(res) {
					var data = res.body;
					if (parseInt(data["code"]) === 0) {
						this.successFun(data["msg"])
						this.getTableData("");
					} else {
						this.errorFun(data['msg'])
					}
				})
			},
			// 搜索数据
			findForm (formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						var d = this.ruleForm;
						var obj = {
							id: d.id,
							gameId: d.gameId,
							gameName: d.gameName,
							groupName: d.addName,
							voiceTemplate: d.addVoice,
							description: d.addDesc,
							notifyType: d.notifyType
						}
						obj.id = '';
						var str = '';
						for(var i in obj){
							if(obj[i] !== ""){
								str += `${i}=${obj[i]}&`
							}
						}
						str = str.substring(0,str.length-1);
						this.getTableData("?"+str);
					} else {
						return false;
					}
				});
			},
			// 修改数据
			handleEdit(index, row) {
				this.revampForm.id = row.id;
				this.revampForm.addName = row.groupName;
				this.revampForm.addVoice = row.voiceTemplate;
				this.revampForm.addDesc = row.description,
				this.revampForm.addWay = row.notifyTypeList;
				this.revampForm.notifyType = row.notifyType;
				this.revampShow = true;
			},
			/*修改提交*/
			revampComit(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						var d = this.revampForm;
						var obj = {
							gameId: 0,
							id: d.id,
							groupName: d.addName,
							voiceTemplate: d.addVoice,
							description: d.addDesc,
							notifyType: d.notifyType
						}
						this.$http.post(`${this.SRC}monitorgroup/modify.do`,obj,{emulateJSON:true}).then(function(res){
							var data = res.body;
							if(parseInt(data['code']) === 0){
								this.successFun(data['msg']);
								this.getTableData("");
							}else{
								this.errorFun(data['msg'])
							}
						})
					} else {
						return false;
					}
				});
			},
			// 删除数据
			handleDelete(index, row) {
				var tableDataCP = this.tableData;
				this.tableData.splice(index, 1);
				this.$http.get(`${this.SRC}monitorgroup/delete.do?id=${row.id}`).then(function(res) {
					var data = res.body
					if (parseInt(data['code']) === 0) {
						this.successFun(data['msg']);
						this.getTableData("");
					} else {
						this.errorFun(data['code']);
						this.tableData = this.tableDataCP;
					}
				})
			},
			/*表格数据*/
			getTableData(src) {
				var that = this;

				function post(url) {
					return new Promise(
						function(resolve, reject) {
							that.$http.post(that.SRC + url).then(function(res) {
								if (res.body['code'] === 0) {
									resolve(res.body['msg']);
								} else {
									reject(false);
								}
							})
						}
					)
				};
				var urlData = ["project/list.do", "monitorgroup/list.do" + src];

				function renderAll() {
					return Promise.all([post(urlData[0]), post(urlData[1])]);
				};
				renderAll().then(function(value) {
					console.log(value)
					that.games = value[0];
					that.dataTotal = value[1].length;
					that.tableDataAll = value[1];
					that.tableControl();
				})
			}
		},
		mounted() {
			this.SRC = this.$store.getters.src3;
			this.getTableData("");
		}
}